import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:kxy_flutter_app/auth.dart';
import 'package:kxy_flutter_app/helper/http.dart';
import 'package:kxy_flutter_app/model/exam_question.dart';
import 'package:kxy_flutter_app/r.dart';

class ReviewBottomBarWidget extends StatefulWidget {
  final SwiperController swiperCtrl;
  final Function complete;
  final int index;
  final int totalCount;
  final Question question;
  final bool withCollect;

  const ReviewBottomBarWidget({
    Key key,
    @required this.swiperCtrl,
    @required this.complete,
    @required this.index,
    @required this.totalCount,
    @required this.question,
    this.withCollect = true,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _ReviewBottomBarWidget();
  }
}

class _ReviewBottomBarWidget extends State<ReviewBottomBarWidget> {
  @override
  Widget build(BuildContext context) {
    var border = Border(
      top: BorderSide(
        color: Color.fromRGBO(240, 240, 240, 1),
        width: 1,
      ),
    );

    return Container(
      height: 50,
      width: MediaQuery.of(context).size.width,
      decoration: BoxDecoration(border: border),
      padding: EdgeInsets.symmetric(horizontal: 0),
      child: body(context),
    );
  }

  Widget body(BuildContext context) {
    return Row(
      children: [
        Expanded(child: btnPre(context)),
        if (widget.withCollect) Expanded(child: btnCollect(context)),
        Builder(builder: (_) {
          if (widget.question.confirmed) {
            return Expanded(child: btnNext(context));
          }
          return Expanded(child: btnConfirm(context));
        }),
      ],
    );
  }

  BoxDecoration decoration() => BoxDecoration(
        color: Colors.white,
        border: Border(
          right: BorderSide(
            color: Color.fromRGBO(240, 240, 240, 1),
          ),
        ),
      );

  TextStyle labelStyle(Color color) => TextStyle(
        fontSize: 14,
        color: color,
        fontWeight: FontWeight.w500,
      );

  Widget btnPre(BuildContext context) {
    var isBegin = widget.index == 0;
    return GestureDetector(
      child: Container(
        height: 50,
        decoration: decoration(),
        alignment: Alignment.center,
        child: Text(
          '上一题',
          style: labelStyle(
            isBegin ? Colors.black54 : Colors.black,
          ),
        ),
      ),
      onTap: () {
        if (isBegin) return;
        widget.swiperCtrl.previous();
      },
    );
  }

  Widget btnNext(BuildContext context) {
    var isEnd = widget.index == widget.totalCount - 1;

    return GestureDetector(
      child: Container(
        height: 50,
        color: Colors.white,
        alignment: Alignment.center,
        child: Text(
          '下一题',
          style: labelStyle(
            isEnd ? Colors.black54 : Colors.black,
          ),
        ),
      ),
      onTap: () {
        if (isEnd) return;
        widget.swiperCtrl.next();
      },
    );
  }

  Widget btnConfirm(BuildContext context) {
    return GestureDetector(
      child: Container(
        height: 50,
        color: Theme.of(context).primaryColor,
        alignment: Alignment.center,
        child: Text('确认', style: labelStyle(Colors.white)),
      ),
      onTap: () {
        if (widget.question.answered) {
          widget.complete();
        }
      },
    );
  }

  Widget btnCollect(BuildContext context) {
    var isCollect = widget.question.isCollect;
    return GestureDetector(
      child: Container(
        height: 50,
        decoration: decoration(),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: EdgeInsets.only(bottom: 3),
              child: Image.asset(
                isCollect ? R.assetsCommonCollect : R.assetsCommonUncollect,
                height: 20,
              ),
            ),
            Text(
              '收藏',
              style: TextStyle(
                fontSize: 11,
                color: Colors.black54,
              ),
            ),
          ],
        ),
      ),
      onTap: () {
        widget.question.isCollect ? uncollect() : collect();
      },
    );
  }

  Future uncollect() async {
    var res = await Http.instance.post('/exam-app/coll/unCollQues', params: {
      'quesId': widget.question.quesId,
      'userId': Auth.instance.userId,
    });
    if (res.code == 200) {
      Fluttertoast.showToast(msg: '取消收藏成功');
      setState(() {
        widget.question.isCollect = false;
      });
    } else {
      Fluttertoast.showToast(msg: res.msg);
    }
  }

  Future collect() async {
    var res = await Http.instance.post('/exam-app/coll/collQues', params: {
      'quesId': widget.question.quesId,
      'userId': Auth.instance.userId,
    });
    if (res.code == 200) {
      Fluttertoast.showToast(msg: '收藏成功');
      setState(() {
        widget.question.isCollect = true;
      });
    } else {
      Fluttertoast.showToast(msg: res.msg);
    }
  }
}
